package view.create
{
	import com.greensock.TimelineMax;
	import com.greensock.TweenMax;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	
	import org.osflash.signals.Signal;
	
	import subumbo.leg.model.Lib;
	import subumbo.leg.model.vo.ResizeVO;
	import subumbo.leg.view.ViewComponent;
	
	import view.create.behind.Skin;

	public class Create extends ViewComponent
	{
		public var save:Signal = new Signal(Array, String);
		
		private var _body:Skin;
		private var _sticker:Skin;
		private var _wheelL:Skin;
		private var _wheelR:Skin;
		
		private var _detailBox:MovieClip;
		
		private var _skins:Sprite;
		
		private var _outline:Sprite;
		
		private var _assemble:MovieClip;
		private var _save:MovieClip;
		
		private var _timeline:TimelineMax;
		
		public override function construct():void {
			
			x =  0;
			y = 60;
			
		
			
			_skins = new Sprite();
			addChild(_skins);
							
			_wheelL = Skin(Lib.getMovieClip("Wheel"));
			_wheelL.init(true);
			_wheelL.instructionText = "Upload a jpg, png or swf";
			_wheelL.y = 320;
			_wheelL.x = 250;
			_wheelL.scaleX = _wheelL.scaleY = .5;
			_skins.addChild(_wheelL);
			
			_wheelR = Skin(Lib.getMovieClip("Wheel"));
			_wheelR.init(true);
			_wheelR.instructionText = "Upload a jpg, png or swf";
			_wheelR.y = 320;
			_wheelR.x = 420;
			_wheelR.scaleX = _wheelR.scaleY = .5;
			_skins.addChild(_wheelR);
			
			_body = Skin(Lib.getMovieClip("BODY"));
			_body.init();
			_body.instructionText = "Upload a jpg, png or swf";
			_body.y = 10;
			_body.x = 135;
			_body.scaleX = _body.scaleY = .5;
			_skins.addChild(_body);
			
			_sticker = Skin(Lib.getMovieClip("Sticker"));
			_sticker.init();
			_sticker.instructionText = "Upload a jpg, png or swf";
			_sticker.scaleX = _sticker.scaleY = .5;
			_sticker.y = 10;
			_sticker.x = 550;
			_skins.addChild(_sticker);
			
			
			
			
			_outline = new Sprite();
			_skins.addChild(_outline);
			with(_outline.graphics) {
				clear();
				lineStyle(5,0x999999);
				beginFill(0xFFFFCD,.9);
				drawRoundRect(125, 0, 815, 523, 50, 50);
			}
			_outline.mouseEnabled = false;
			_outline.alpha = 0;
			
			_save = Lib.getMovieClip("Button");
			_save.x = 460;
			_save.y = 200;
			_save.visible = false;
			_save.mouseChildren = false;
			_save.buttonMode = true;
			_save.txt.autoSize = TextFieldAutoSize.LEFT;
			_save.txt.text = "save";
			_save.bg.width = _save.txt.width + 50;
			_skins.addChild(_save);
			
			
			_assemble = Lib.getMovieClip("Button");
			_assemble.x = 10;
			_assemble.y = 10;
			_assemble.buttonMode = true;
			_assemble.mouseChildren = false;
			_assemble.scaleX = _assemble.scaleY = .4;
			_assemble.txt.autoSize = TextFieldAutoSize.LEFT;
			_assemble.txt.text = "assemble"
			_assemble.bg.width = _assemble.txt.width + 50;
			_assemble.addEventListener(MouseEvent.MOUSE_DOWN, onAssemble);
			addChild(_assemble);
			
			
			_detailBox = Lib.getMovieClip("DetailBox");
			_detailBox.visible = false;
			_detailBox.submit.txt.text = "submit";
			addChild(_detailBox);
			
			
			_timeline = new TimelineMax();
			var tweens:Array = [TweenMax.to(_wheelL, .2, {scaleX:1, scaleY:1, x:364, y:239}),
								TweenMax.to(_wheelR, .2, {scaleX:1, scaleY:1, x:699, y:239, delay:.1}),
								TweenMax.to(_body, .2, {scaleX:1, scaleY:1, delay:.3}),
								TweenMax.to(_sticker, .2, {scaleX:1, scaleY:1, x:168, y:40, delay:.5})];
			_timeline.insertMultiple(tweens);
			_timeline.stop();
						
		}
		
		private function onAssemble(e:MouseEvent):void {
			if(_assemble.txt.text == "assemble") {
				_assemble.txt.text = "disassemble";
				_wheelL.uploadEnabled = false;
				_wheelR.uploadEnabled = false;
				_sticker.uploadEnabled = false;
				_body.uploadEnabled = false;
				_skins.addEventListener(MouseEvent.ROLL_OVER, onMouse);
				_skins.addEventListener(MouseEvent.ROLL_OUT, onMouse);
				_skins.mouseEnabled = false;
				_save.addEventListener(MouseEvent.MOUSE_DOWN, onMouse);
				_timeline.play();
		
			}else {
				_assemble.txt.text = "assemble";
				_wheelL.uploadEnabled = true;
				_wheelR.uploadEnabled = true;
				_sticker.uploadEnabled = true;
				_body.uploadEnabled = true;
				_skins.mouseEnabled = true;
				_skins.removeEventListener(MouseEvent.ROLL_OVER, onMouse);
				_skins.removeEventListener(MouseEvent.ROLL_OUT, onMouse);
				_save.removeEventListener(MouseEvent.MOUSE_DOWN, onMouse);
				
				_timeline.reverse();
			}
			_assemble.bg.width = _assemble.txt.width + 50;
		}
		
		private function onSubmit(e:MouseEvent):void {
			
			TweenMax.to(_detailBox, .2 , {autoAlpha:0});
			var list:Array = [];
			list.push(_wheelL.fileReference);
			list.push(_wheelR.fileReference);
			list.push(_sticker.fileReference);
			list.push(_body.fileReference);
					
			save.dispatch(list, _detailBox.input.text);
		}
		
		private function onMouse(e:MouseEvent):void {
			switch(e.type) {
				case MouseEvent.MOUSE_DOWN: 
					
					_detailBox.visible = true;
					TweenMax.to(_detailBox, .6 , {autoAlpha:1});
					_detailBox.submit.addEventListener(MouseEvent.MOUSE_DOWN, onSubmit, false, 0, true);
					
					
					
				break;
				
				case MouseEvent.ROLL_OVER :
					_save.visible = true;
					TweenMax.to(_save, .5, {autoAlpha:1});
					TweenMax.to(_outline, .5, {autoAlpha:1});
				break;
				
				case MouseEvent.ROLL_OUT :
					TweenMax.to(_save, .5, {autoAlpha:0});
					TweenMax.to(_outline, .5, {autoAlpha:0});
				break;
			}
		}
		
		public override function resize(resizeVO:ResizeVO):void {
			/* _body.x = resizeVO.width / 100 * 20;
			_sticker.x  = _body.x + 15;
			_wheelL.x = _body.x + _body.width + 100;
			_wheelR.x = _wheelL.x + 100;
			 */
			_detailBox.x = resizeVO.centerX;
			_detailBox.y = resizeVO.centerY;
		}
		
		
	}
}